async function initLogin() {
    console.log(1);
    document.querySelector("#login").innerHTML = `
	<div class="login-section">
		<div class="login-top">登录<p onclick="closeLoginMenu()" id="close-login">❌</p>
		</div>
		<div class="login-form">
			<h2 style="margin: 10px; margin-top: 30px;">用户登录</h2>
			<input type="text" id="email" name="email" placeholder="邮箱">
			<input type="password" id="password" name="password" placeholder="密码" />
			<div class="login-menu">
				<div id="register"><a href="/ForWhatSite/register.html">免费注册</a></div>
				<div id="find-password">忘记密码</div>
			</div>
			<button type="button" onclick="send_login()" id="login-button">登录</button>
			<p id="reminder"></p>
			<h6 class="role">登录即代表同意《服务协议》和《隐私政策》</h6>
		</div>
	</div>
	`
}

async function toLogin(noShowLogin) {
    if (!await checkLogin()) {
        if (noShowLogin) {
            return
        }
        scrollTo(0, 0)
        openLoginMenu()
    }else {

    }
}

// 打开登录界面
async function openLoginMenu() {
    let user = document.querySelector("#login-state")

    if (!await checkLogin()) {
        scrollTo(0, 0)
        let loginShow = document.querySelector("#login")
        loginShow.style.display = 'flex'
        loginShow.style.animation = 'loginShow 1s'
        document.body.parentNode.style.overflow = "hidden"
    } else {
        ajax('post', '/user/logout', "{}", function (json) {
            alert(json.code)
        }, null)
        let loginState = document.querySelector("#login-state")
        loginState.innerHTML = '用户登录'
    }
}


//检查是否登录 true /false
async function checkLogin() {
    let loginState = document.querySelector("#login-state")
    let login = await ajax('post', '/user/login', "{}", function (json) {
        // console.log(json)
        //请求成功
        if (json.code === 200) {
            loginState.innerHTML = json.data.userName
            return true
        }
        loginState.innerHTML = '用户登录'
        return false
    }, null)
    // console.log(login)
    return login
}

// 关闭登录界面
function closeLoginMenu() {
    let loginShow = document.querySelector("#login")
    // loginShow.style.display = 'flex'
    // for (var i = 0; i < 100; i++) {
    // 	setInterval()
    // }
    loginShow.style.animation = 'loginClose 1s'
    document.body.parentNode.style.overflow = "auto"
    setTimeout(function () {
        loginShow.style.display = 'none'
    }, 900)

}


// login
function send_login() {
    let email = document.querySelector("#email").value
    let password = document.querySelector("#password").value

    let postParam = {
        'userEmail': email,
        'userPassword': password
    }

    ajax('post', '/user/login', postParam, function (json) {
        //请求成功
        let loginMenu = document.querySelector("#login")
        let reminder = document.querySelector("#reminder")
        if (json.code == 200) {
            let loginState = document.querySelector("#login-state")
            loginState.innerHTML = json.data.userName
            if (loginMenu.style.display == 'flex') {
                closeLoginMenu()
            }
        }

        if (loginMenu.style.display == 'flex') {
            reminder.innerHTML = json.msg
        }
    }, null)

}

